home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #ifdef __TURBOC__
- #include <conio.h>
- #else
- #include <signal.h>
- #include <sgtty.h>
-
- #define TTY "/dev/tty"
- #endif
-
- /* Issue prompt and read reply with echo turned off */
- char *
- getpass(prompt)
- #ifdef __TURBOC__
- const char *prompt;
- #else
- char *prompt;
- #endif
- {
- #ifndef __TURBOC__
- struct sgttyb ttyb,ttysav;
- FILE *tty;
- int (*signal())(),(*sig)();
- #endif
- register char *cp;
- int c;
- static char pbuf[128];
-
- #ifndef __TURBOC__
- if ((tty = fdopen(open(TTY, 2), "r")) == NULL)
- tty = stdin;
- else
- setbuf(tty, (char *)NULL);
- sig = signal(SIGINT, SIG_IGN);
- ioctl(fileno(tty), TIOCGETP, &ttyb);
- ioctl(fileno(tty), TIOCGETP, &ttysav);
- ttyb.sg_flags |= RAW;
- ttyb.sg_flags &= ~ECHO;
- ioctl(fileno(tty), TIOCSETP, &ttyb);
- #endif
-
- fprintf(stderr, "%s", prompt);
- fflush(stderr);
- cp = pbuf;
- for (;;) {
- #ifdef __TURBOC__
- loop: c = getch();
- if (!c) {
- c = getch();
- goto loop;
- }
-
- if (c == '\r' || c == '\n' || c == EOF)
- break;
-
- if (cp < &pbuf[127])
- *cp++ = c;
- #else
- c = getc(tty);
- if(c == '\r' || c == '\n' || c == EOF)
- break;
- if (cp < &pbuf[127])
- *cp++ = c;
- #endif
- }
- *cp = '\0';
- #ifdef __TURBOC__
- fprintf(stderr,"\n");
- #else
- fprintf(stderr,"\r\n");
- #endif
- fflush(stderr);
-
- #ifndef __TURBOC__
- ioctl(fileno(tty), TIOCSETP, &ttysav);
- signal(SIGINT, sig);
- if (tty != stdin)
- fclose(tty);
- #endif
- return(pbuf);
- }